home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / rdataclass.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.9 KB  |  92 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''DNS Rdata Classes.
  5.  
  6. @var _by_text: The rdata class textual name to value mapping
  7. @type _by_text: dict
  8. @var _by_value: The rdata class value to textual name mapping
  9. @type _by_value: dict
  10. @var _metaclasses: If an rdataclass is a metaclass, there will be a mapping
  11. whose key is the rdatatype value and whose value is True in this dictionary.
  12. @type _metaclasses: dict'''
  13. import re
  14. import dns.exception as dns
  15. RESERVED0 = 0
  16. IN = 1
  17. CH = 3
  18. HS = 4
  19. NONE = 254
  20. ANY = 255
  21. _by_text = {
  22.     'RESERVED0': RESERVED0,
  23.     'IN': IN,
  24.     'CH': CH,
  25.     'HS': HS,
  26.     'NONE': NONE,
  27.     'ANY': ANY }
  28. _by_value = []([ (y, x) for x, y in _by_text.iteritems() ])
  29. _by_text.update({
  30.     'INTERNET': IN,
  31.     'CHAOS': CH,
  32.     'HESIOD': HS })
  33. _metaclasses = {
  34.     NONE: True,
  35.     ANY: True }
  36. _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I)
  37.  
  38. class UnknownRdataclass(dns.exception.DNSException):
  39.     '''Raised when a class is unknown.'''
  40.     pass
  41.  
  42.  
  43. def from_text(text):
  44.     '''Convert text into a DNS rdata class value.
  45.     @param text: the text
  46.     @type text: string
  47.     @rtype: int
  48.     @raises dns.rdataclass.UnknownRdataClass: the class is unknown
  49.     @raises ValueError: the rdata class value is not >= 0 and <= 65535
  50.     '''
  51.     value = _by_text.get(text.upper())
  52.     if value is None:
  53.         match = _unknown_class_pattern.match(text)
  54.         if match == None:
  55.             raise UnknownRdataclass
  56.         
  57.         value = int(match.group(1))
  58.         if value < 0 or value > 65535:
  59.             raise ValueError, 'class must be between >= 0 and <= 65535'
  60.         
  61.     
  62.     return value
  63.  
  64.  
  65. def to_text(value):
  66.     '''Convert a DNS rdata class to text.
  67.     @param value: the rdata class value
  68.     @type value: int
  69.     @rtype: string
  70.     @raises ValueError: the rdata class value is not >= 0 and <= 65535
  71.     '''
  72.     if value < 0 or value > 65535:
  73.         raise ValueError, 'class must be between >= 0 and <= 65535'
  74.     
  75.     text = _by_value.get(value)
  76.     if text is None:
  77.         text = 'CLASS' + `value`
  78.     
  79.     return text
  80.  
  81.  
  82. def is_metaclass(rdclass):
  83.     '''True if the class is a metaclass.
  84.     @param rdclass: the rdata class
  85.     @type rdclass: int
  86.     @rtype: bool'''
  87.     if _metaclasses.has_key(rdclass):
  88.         return True
  89.     
  90.     return False
  91.  
  92.